using System.Linq.Expressions;
using FastDevTemplate.Api.Attribute;
using FastDevTemplate.Api.Base;
using FastDevTemplate.Api.Model;
using FastDevTemplate.Api.Model.Base;
using FastDevTemplate.DataAccess.Entities;
using FastDevTemplate.Service;
using FastDevTemplate.Service.Interface;
using FastDevTemplate.Service.Interface.Base;
using FastDevTemplate.Service.Interface.DTO;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace FastDevTemplate.Api.Controllers;

[EnableCors("AllowSpecificOrigin")]
public class UserController : BaseController
{
    private readonly string? _defaultRoleType;
    private readonly IUserService _userService;

    public UserController(IUserService userService, IConfiguration config)
    {
        _userService = userService;
        _defaultRoleType = config["AddUserDefaultRoleType"];
    }

    [HttpPost("add")]
    [PermissionVerification(id:"uc/user/add", describe:"用户添加权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult<KeyModel<Guid>>> Add([FromBody] UserAddDto model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult<KeyModel<Guid>>();
        var result = await _userService.AddAsync(model);
        if (result == null)
            return BadResult<KeyModel<Guid>>();
        if (_defaultRoleType != null)
            await _userService.SetRoleAsync(result.ID, _defaultRoleType);
        return OkResult(new KeyModel<Guid>(result.ID));
    }

    [HttpPost("modifyInfo")]
    [PermissionVerification(id:"uc/user/modify", describe:"用户修改权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult> ModifyInfo([FromBody] ModifyRequestModel<Guid, UserUpdateDto> model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult();
        var result = await _userService.ModifyAsync(model.ID, model.Data);
        if (result == null)
            return BadResult();
        TokenService.RemoveToken(model.ID);
        return OkResult();
    }

    [HttpPost("remove")]
    [PermissionVerification(id:"uc/user/remove", describe:"用户删除权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult> Remove([FromBody] KeyModel<Guid> model)
    {
        var result = await _userService.RemoveAsync(model.ID);
        if (result == null)
            return BadResult();
        TokenService.RemoveToken(model.ID);
        return OkResult();
    }
    
    [HttpPost("recover")]
    [PermissionVerification(id:"uc/user/recover", describe:"用户恢复权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult> Recover([FromBody] KeyModel<Guid> model)
    {
        var result = await _userService.RecoverAsync(model.ID);
        if (result < 1)
            return BadResult();
        return OkResult();
    }
    
    [HttpPost("getByID")]
    [PermissionVerification(id:"uc/user/get", describe:"用户获取权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult<UserGetResultDto>> GetByID([FromBody] KeyModel<Guid> model)
    {
        var result = await _userService.GetByIdAsync(model.ID);
        return CheckResult(result);
    }

    [HttpPost("getListByQuery")]
    [PermissionVerification(id:"uc/user/get", describe:"用户获取权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult<PageResultDto<UserResultDto>>> GetListByQuery(
        [FromBody] PageRequestModel<UserBlurQueryItem> model)
    {
        if (model.QueryItem != null && !ValidateModel(model.QueryItem).isValid)
            return BadResult<PageResultDto<UserResultDto>>();

        var expressionList = DeserializeQueryParameters<User>(model.QueryItem, ExpressionType.Contains);

        var result = await _userService.GetPageAsync(
            model.PageIndex,
            model.PageSize,
            expressionList.ToArray());
        
        return CheckResult(result);
    }

    [HttpPost("setRoleList")]
    [PermissionVerification(id:"uc/user/set-role", describe:"用户设置角色权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult> SetRole([FromBody] ModifyRequestModel<Guid, string[]> model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult();
        var result = await _userService.SetRoleAsync(model.ID, model.Data);
        if (result < 1)
            return BadResult();
        return OkResult();
    }

    [HttpPost("addRoleList")]
    [PermissionVerification(id:"uc/user/set-role", describe:"用户设置角色权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult> AddRole([FromBody] ModifyRequestModel<Guid, string[]> model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult();
        var result = await _userService.AddRoleListAsync(model.ID, model.Data);
        if (result < 1)
            return BadResult();
        return OkResult();
    }

    [HttpPost("removeRoleList")]
    [PermissionVerification(id:"uc/user/set-role", describe:"用户设置角色权限", client:"uc")]
    [LogRecord]
    public async Task<HttpResult> RemoveRole([FromBody] ModifyRequestModel<Guid, string[]> model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult();
        var result = await _userService.RemoveRoleListAsync(model.ID, model.Data);
        if (result < 1)
            return BadResult();
        return OkResult();
    }
}